package team.tunan.controller;


import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import team.tunan.common.LayUIResult;
import team.tunan.common.Result;
import team.tunan.entity.Matches;
import team.tunan.service.IMatchesService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
@Controller
//@RequestMapping("/matches")
@RequestMapping("match")
public class MatchesController {

    @Autowired
    private IMatchesService matchService;

    @RequestMapping("/list")
    public String matchList(){
        return "match/manager";
    }

    @RequestMapping("/control")
    public String control(){
        return "match/control";
    }

    @RequestMapping("/data")
    @ResponseBody
    public LayUIResult data(Integer gid, Integer roundNum, Model model){
        System.out.println(gid + " " + roundNum);
        List<Map<String,Object>> matchInfo = matchService.getMatchInfo(gid, roundNum);
        long count = matchInfo.stream().count();
        if (matchInfo==null){

            return new LayUIResult(1,"失败",0,null);
        }
        return new LayUIResult(0,"成功",count,matchInfo);
    }

    @RequestMapping("/add")
    public String add(){
        return "match/add";
    }
    @RequestMapping("/addMatches")
    public String addMatches(){
        return "match/addMatches";
    }
    @RequestMapping("/addGame")
    public String addGame(){
        return "match/addGame";
    }

    @RequestMapping("/doAdd")
    @ResponseBody
    public Result doAdd(Matches matches){
        System.out.println(matches);
        boolean end = matchService.isEnd(matches);
        System.out.println("end======>>>>"+end);
        if(!end){
            return new Result("上一轮比赛场次还没结束",null,true);
        }
        int i = matchService.doAdd(matches);
//        int i =1;

        return new Result(i>0?"成功":"失败",null,i>0);
    }




//    @RequestMapping("/playInfo")
//    @ResponseBody
//    public LayUIResult getplayinfo(){
//        List<Map<String, Object>> playerInfo = matchService.getPlayerInfo();
//        int size = playerInfo.size();
//        return new LayUIResult(0,null,size,playerInfo);
//    }

    /**
     * 根据传输过来的gid判断状态进行返回哪种类型的选手
     * @param gid
     * @return
     */
    @RequestMapping("/playInfo/{gid}")
    @ResponseBody
    public LayUIResult getplayinfo(@PathVariable Integer gid){
        List<Map<String, Object>> playerInfo = matchService.getPlayerInfo(gid);
        int size = playerInfo.size();
        return new LayUIResult(0,null,size,playerInfo);
    }

    /**
     * 根据id获取要修改的场次数据进行回显
     * @param mid
     * @param model
     * @return
     */
    @RequestMapping("/edit/{mid}")
    public String e(@PathVariable Integer mid,Model model){
        Matches match = matchService.getMatch(mid);
        model.addAttribute("match",match);
        return "match/editmatch";
    }






    @RequestMapping("/start")
    @ResponseBody
    public Result startMatch(Matches matches){
        System.out.println("matches = " + matches);
        Map<String,Object>  map = matchService.startMatches(matches);

        boolean flag = (boolean)map.get("flag");
        Integer status = (Integer)map.get("status");

        String msg = null;
        if (status==0){
            msg = "已开启";
        }else if (status==1){
            msg = "比赛正在进行中......";
        }else if (status==2){
            msg = "比赛已结束......";
        }

        return new Result(msg,map,flag);
    }

    @RequestMapping("/end")
    @ResponseBody
    public Result endMatch(Matches matches){
        Integer mid = matches.getMid();

        Map<String,Object>  map = matchService.endMatches(mid);

        System.out.println("===================");
        System.out.println("map" + map);

        boolean flag = (boolean)map.get("flag");

        Integer status = (Integer)map.get("status");

        String msg = null;
        if (status==0){
            msg = "比赛未开启";
        }else if (status==1){
            msg = "已关闭";
        }else if (status==2){
            msg = "比赛已结束......";
        }

//        boolean b =false;
        return new Result(msg,map,flag);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update( Matches matches){
        System.out.println("match =========>"+matches);
        Map<String, Object> map = matchService.updateMatch(matches);
        boolean flag = (boolean)map.get("flag");
        Integer status = (Integer)map.get("status");
        String msg = null;
        if (status == 0){
            msg = "比赛场次开启";
        }else if(status ==1){
            msg = "场次活动未开始不能直接结束或暂停";
        }else if(status == 2){
            msg = "比赛场次已结束";
        }else if(status == 3){
            msg = "比赛场次已暂停";
        }
        else if(status == 4){
            msg = "比赛场次所属比赛还未开始";
        }

        return new Result(msg,map,flag);
    }

    @RequestMapping("/maxRoundNum/{gid}")
    @ResponseBody
    public Result maxRoundNum(@PathVariable Integer gid){
        Integer i = matchService.maxRoundNum(gid);
        if(i>0){
            return new Result(null,i,true);
        }
        else{
            return new Result(null,i,false);
        }
    }

}

